package cn.tedu.luban.account.main.service;

import cn.tedu.luban.account.main.converter.AccountConverter;
import cn.tedu.luban.account.main.repository.AccountRepository;
import cn.tedu.luban.account.protocol.bo.AccountBO;
import cn.tedu.luban.account.protocol.dto.AccountDTO;
import cn.tedu.luban.account.protocol.param.AccountCreateParam;
import cn.tedu.luban.account.protocol.param.AccountPayParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountService {
    @Autowired
    private AccountRepository accountRepository;
    public Long createAccount(AccountCreateParam accountCreateParam) {
        Long result=0L;
        //1. 使用userId查询是否存在账号 count命令 select count
        Long userId = accountCreateParam.getUserId();
        AccountBO accountBO = accountRepository.getAccountByUserId(userId);
        //判断
        if (accountBO==null||accountBO.getId()==null){
            //2. 创建账号
            result=accountRepository.save(accountCreateParam);
        }
        return result;
    }
    @Autowired
    private AccountConverter accountConverter;
    public AccountDTO getAccount(Long userId) {
        //直接调用仓储层 仓储层调用持久层 将数据封装转化返回
        AccountBO bo = accountRepository.getAccountByUserId(userId);
        return accountConverter.bo2dto(bo);
    }

    public Long mockPay(AccountPayParam param) {
        return accountRepository.updateTotalAmount(param);
    }
}
